package light.head.controller.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.ProcessConfigDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.model.ProcessConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/***
 * 流程配置
 * @author yy
 *
 */
@Controller
@RequestMapping(Url.C_PROCESSCONFIG)

public class ProcessConfigController extends SupporterController{
	private ProcessConfigDAO pcDAO;
	private OrganDAO organDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.PROCESSCONFIG_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		List<ProcessConfig> list = pcDAO.findByOc(organDAO.getRoot(cu_organCode(request)).getCode());
		if (null != list && list.size() > 0) {
			
			List<Byte> businessList = new ArrayList<Byte>();
			Byte type = 0;
			List<ProcessConfig> resultList = new ArrayList<ProcessConfig>();
			
			List<ProcessConfig> itemList = null;
			for (ProcessConfig p1 : list) {
				if (type.byteValue() == p1.getBusiness().byteValue()) continue;
				
				itemList = new ArrayList<ProcessConfig>();
				for (ProcessConfig p2 : list) {
					if (p1.getBusiness().byteValue() == p2.getBusiness().byteValue()) {
						itemList.add(p2);
					}
				}
				p1.setItemList(itemList);
				resultList.add(p1);
				businessList.add(p1.getBusiness());
				
				type = p1.getBusiness();
			}
			request.setAttribute(Parm.LIST, resultList);
			request.setAttribute(Parm.LIST_1, businessList.toString());		// 已经有的业务类型
		}
		return new ModelAndView(Url.PROCESSCONFIG_LIST);
	}
	
	
	
	@Transactional(rollbackFor = Exception.class)
	@RequestMapping(Url.SAVE)
	public void save(ProcessConfig pc, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if (null == pc) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		List<ProcessConfig> itemList = pc.getItemList(); // 页面获取的审核角色
		
		// 判断是否要相同的角色
		int j = 0;
		for (ProcessConfig p : itemList) {
			j = 0;
			for (ProcessConfig p1 : itemList) {
				if (null != p.getRc() && null != p1.getRc() && p.getRc().intValue() == p1.getRc().intValue()) {
					j++;
				}
				if (j > 1) {
					print(response, Msg.ERROR);		// 角色重复，不能保存
					return;
				}
			}
		}
		// 1.数据库查询出来的审核角色
		List<ProcessConfig> dbList = pcDAO.findByOcBusiness(organDAO.getRoot(cu_organCode(request)).getCode(), pc.getBusiness());
		// 2. 比较数据库查询出来的有不在页面提交上的就设deleted 为true
		List<ProcessConfig> deletedList = new ArrayList<ProcessConfig>();	// 存放页面已经删除的角色节点
		// 找出页面删除的角色节点
		Boolean ok = null;
		for (ProcessConfig dbp : dbList) {
			ok = true;
			for (ProcessConfig p : itemList) {
				if (null != p.getCode() && p.getCode().intValue() == dbp.getCode().intValue()) {
					ok = false;
					break;
				}
			} 
			if (ok) {
				deletedList.add(dbp);
			}
		}
		// 删除角色节点(在数据库中deleted = true)
		for (ProcessConfig dbp : deletedList) {
			dbp.setDeleted(true);
			result = pcDAO.upd(dbp);
			if (RESULT.SUCCESS != result) {
				throw new Exception("保存失败"); 
			}
		}
		
		// 保存流程配置
		Integer cu = cu_code(request);
		Date ct = new Date();
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		Boolean deleted = false;
		
		int i = 1;
		for (ProcessConfig p : itemList) {
			if (null == p.getRc()) 
				continue;
			
			if (null != p.getCode()) {
				p.setPc(i);
				result = pcDAO.update(p);
				if (RESULT.SUCCESS != result) {
					throw new Exception("保存失败");
				}
			} else {
				p.setCu(cu);
				p.setCt(ct);
				p.setOc(oc);
				p.setDeleted(deleted);
				p.setPc(i);
				p.setBusiness(pc.getBusiness());
				result = pcDAO.add(p);
				if (RESULT.SUCCESS != result) {
					throw new Exception("保存失败");
				}
			}
			i++;
		}
		
		if (RESULT.SUCCESS == result)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	
	@Autowired
	public void setPcDAO(ProcessConfigDAO pcDAO) {
		this.pcDAO = pcDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}

}
